package com.hluck.mvvmlibrary.base;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @Author： LJH
 * @Time： 2023/12/27
 * @description：
 */
public abstract class BaseVBActivity<VB extends ViewBinding> extends BaseActivity {

    protected VB viewBinding;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        onRegister();
        super.onCreate(savedInstanceState);

        Type type = this.getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            try {
                Class<? extends ViewBinding> vbClass = (Class<? extends ViewBinding>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
                Method inflateMethod = vbClass.getMethod("inflate", LayoutInflater.class);
                viewBinding = (VB) inflateMethod.invoke(null, getLayoutInflater());
                setContentView(viewBinding.getRoot());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        initData();

    }

    protected void onRegister() {

    }

    protected abstract void initData();

    /**
     * 设置全屏沉浸式
     */
    protected void setFullScreenImmersion() {
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
        window.getDecorView().setSystemUiVisibility(option);
        window.setStatusBarColor(Color.TRANSPARENT);
        window.setNavigationBarColor(Color.TRANSPARENT);
    }
}
